import os
import sys
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from cgai_img.format import img2ico,img2icns


class DragEdit(QLineEdit):
    def __init__(self):
        super(DragEdit,self).__init__()

        self.format_filter = ['.jpg','.jpeg','.png']   
        self.setDragEnabled(True)

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls:
            event.accept()
            text = event.mimeData().text()
            self._get_mp(text)
            event.ignore()
            # self.drag_enter_Signal.emit(self._get_paths())

        return super().dragEnterEvent(event)

    def _get_mp(self,text):
        mp_paths = []
        if text:
            paths = text.split("\n")
            for i in paths:
                ender = os.path.splitext(i)[-1]
                if ender in self.format_filter:
                    mp = i.replace("file:///",'')
                    mp_paths.append(mp)
                    print('mp:',mp)
        if mp_paths:
            # self.clear()
            self.setText(mp_paths[0])


# class 



class MainWindow(QWidget):

    def __init__(self):
        super(MainWindow,self).__init__()

        FONT_M_12 = QFont('', 12, 75)
        UE_COLOR_STYLES = "background-color:#121212;color:rgb(200,200,200)"
        
        BTN_STYLE_UE_PADDING5 = '''QPushButton{color:#C8C8C8}
                                QPushButton:hover{color:#FF7F24}
                                QPushButton{background-color:#121212}
                                QPushButton{border:2px}
                                QPushButton{border-radius:3px}
                                QPushButton{padding:5px 5px}'''

        self.setWindowTitle("CGAI-图片转ico/icns图标")
        self.setStyleSheet(UE_COLOR_STYLES)
        # self.setFixedSize(QSize(850, 850))
        self.setFixedWidth(850)

        # self.clip_menu = self.menuBar().addMenu('裁剪')
        # self.clip_action = QAction('图片裁剪',self)
        # self.clip_menu.addAction(self.clip_action)

        self.main_widget = QWidget()
        self.main_layout = QVBoxLayout(self.main_widget)
        self.main_layout.setAlignment(Qt.AlignTop)
        self.main_layout.setSpacing(20)
        self.main_title = QLabel('CGAI 图片转ico/icns图标')
        self.main_title.setFont(FONT_M_12)
        self.main_title.setAlignment(Qt.AlignCenter)

        self.convert_type_layout = QHBoxLayout()
        self.convert_type_label = QLabel('转换类型:')
        self.convert_ico_radio = QRadioButton('ico')
        self.convert_ico_radio.setChecked(True)
        self.convert_icns_radio = QRadioButton('icns')
        self.convert_type_layout.addWidget(self.convert_type_label)
        self.convert_type_layout.addWidget(self.convert_ico_radio)
        self.convert_type_layout.addWidget(self.convert_icns_radio)

        self.size_layout = QHBoxLayout()
        self.size_label = QLabel('转换尺寸:')
        self.size_comb = QComboBox()
        self.init_comb()
        self.size_layout.addWidget(self.size_label)
        self.size_layout.addWidget(self.size_comb)

        self.img_layout = QHBoxLayout()
        self.img_label = QLabel('图片路径:')
        self.img_edit = DragEdit()
        self.img_btn = QPushButton('选择')
        self.img_btn.setStyleSheet(BTN_STYLE_UE_PADDING5)
        self.img_btn.clicked.connect(self._choose)
        self.convert_btn = QPushButton('转换')
        self.convert_btn.clicked.connect(self._convert)
        self.convert_btn.setStyleSheet(BTN_STYLE_UE_PADDING5)
        self.img_layout.addWidget(self.img_label)
        self.img_layout.addWidget(self.img_edit)
        self.img_layout.addWidget(self.img_btn)
        self.img_layout.addWidget(self.convert_btn)

        self.help_label = QLabel('使用说明: \n\n1.拖入或者选择图片\n\n2.转换后会在原图片同级目录生成同名的图标')

        self.clip_layout = QHBoxLayout()
        self.clip_label = QLabel('图片目录:')
        self.clip_edit = DragEdit()
        self.dir_btn = QPushButton('选择')
        self.dir_btn.setStyleSheet(BTN_STYLE_UE_PADDING5)
        self.dir_btn.clicked.connect(self._choose_dir)
        self.clip_layout.addWidget(self.clip_label)
        self.clip_layout.addWidget(self.clip_edit)
        self.clip_layout.addWidget(self.dir_btn)

        self.area_layout = QHBoxLayout()
        self.area_layout.setAlignment(Qt.AlignLeft)
        self.edit_width = 50
        self.w_label = QLabel('宽度:')
        self.w_edit = QLineEdit()
        self.w_edit.setFixedWidth(self.edit_width)
        self.h_label = QLabel('高度:')
        self.h_edit = QLineEdit()
        self.h_edit.setFixedWidth(self.edit_width)
        self.clip_btn = QPushButton('裁剪')
        self.clip_btn.clicked.connect(self._clip)
        self.clip_btn.setStyleSheet(BTN_STYLE_UE_PADDING5)
        self.area_layout.addWidget(self.w_label)
        self.area_layout.addWidget(self.w_edit)
        self.area_layout.addWidget(self.h_label)
        self.area_layout.addWidget(self.h_edit)
        self.area_layout.addWidget(self.clip_btn)
        self.area_layout.addStretch()


        self.tip_label = QLabel('')
        self.tip_label.setAlignment(Qt.AlignBottom)
        self.tip_label.setStyleSheet('color:#62ff4d')

        

        self.main_layout.addWidget(self.main_title)
        self.main_layout.addLayout(self.convert_type_layout)
        self.main_layout.addLayout(self.size_layout)
        self.main_layout.addLayout(self.img_layout)
        self.main_layout.addWidget(self.help_label)
        self.main_layout.addLayout(self.clip_layout)
        self.main_layout.addLayout(self.area_layout)
        self.main_layout.addWidget(self.tip_label)
        self.setLayout(self.main_layout)

        # self.setCentralWidget(self.main_widget)


    def init_comb(self):

        size_list = ['16x16','24x24','32x32','48x48','64x64','128x128','512x512']
        self.size_comb.addItems(size_list)
        self.size_comb.setCurrentText('128x128')

    def _choose(self):
        openfile_name = QFileDialog.getOpenFileName(self,'选择文件','','图片(*.png;*.jpg;*.jpeg)')
        if openfile_name[0]:
            self.img_edit.setText(openfile_name[0])

    def _choose_dir(self):
        _dir = QFileDialog.getExistingDirectory(self,'选择文件夹')
        if _dir:
            self.clip_edit.setText(_dir)

    def _clip(self):
        w = self.w_edit.text()
        h = self.h_edit.text()
        if w and h:
            print(w,h)
        else:
            QMessageBox.information(None,'提示','请输入完整图片裁剪宽高')
        



    def _convert(self):
        self.tip_label.clear()
        path = self.img_edit.text()
        if os.path.exists(path):
            pre,_ = path.split('.')
            
            img_size_x,img_size_y = self.size_comb.currentText().split('x')
            if self.convert_ico_radio.isChecked():
                des_path = pre + '.ico'
                img2ico(path,des_path,(int(img_size_x),int(img_size_x)))
            else:
                des_path = pre + '.icns'
                img2icns(path,des_path,(int(img_size_x),int(img_size_x)))
            self.tip_label.setText('转换完成')
        else:
            QMessageBox.information(None,"提示","请先选择图")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    m = MainWindow()
    m.show()
    sys.exit(app.exec_())
        